package com.china08.yxyapi.api.manage.stu.mystudy.mistakes;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.mistakes.StuMistakesQuesNote;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.mistakes.StuMistakesQuesNoteRepository;
import com.china08.yxyapi.util.DateToolUtils;

/**
 * 错题本--学生端--错题笔记--上方查看多条笔记（集合）
 * 
 * @author LiuWeidong
 *
 */
@CrossOrigin
@RestController
public class StuMistakesQuesNoteSchController extends YxyApiBaseController {

    @Autowired
    private StuMistakesQuesNoteRepository stuMistakesQuesNoteRepository;

    @RequestMapping(value = "/api_yw/manage/stu/mystudy/mistakes/note/sch", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody ReqModel model) throws ParseException {
        List<RespModel> returnLst = new ArrayList<StuMistakesQuesNoteSchController.RespModel>();
        String userId = model.studentId;
        String quesId = model.quesId;

        List<StuMistakesQuesNote> stuMistakesQuesNoteLst = stuMistakesQuesNoteRepository
                .findByQuesIdAndStudentId(quesId, userId);

        if (stuMistakesQuesNoteLst != null) {
            for (StuMistakesQuesNote stuMistakesQuesNote : stuMistakesQuesNoteLst) {
                RespModel respModel = new RespModel();
                respModel.id = stuMistakesQuesNote.getId();
                respModel.note = stuMistakesQuesNote.getNote();
                String aa = DateToolUtils.format(stuMistakesQuesNote.getPublishDate(), DateToolUtils.FORMAT_LONG);
                respModel.publishDate = aa;
                returnLst.add(respModel);
            }
        }

        return returnLst;
    }

    /* 请求参数 */
    protected static class ReqModel extends YxyApiReqModel {

        // 学生id
        public String studentId;

        // 试题:ID
        public String quesId;
    }

    /* 响应参数 */
    protected static class RespModel extends YxyApiRespModel {

        // 笔记id
        public String id;

        // 笔记内容
        public String note;

        // 笔记发布时间
        public String publishDate;
    }
}
